package com.pingjiao.operation.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pingjiao.commonutils.R;
import com.pingjiao.operation.pojo.DbStudent;
import com.pingjiao.operation.pojo.VO.StudentMessage;
import com.pingjiao.operation.service.DbStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author Liam
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/operation/db-student")
@Api(tags = "学生有关的接口")
@CrossOrigin
public class DbStudentController {
    @Autowired
    DbStudentService dbStudentService;

    //1.查询所有学生的信息
    @GetMapping("selectAllStudent")
    @ApiOperation(value = "查询所有学生信息的接口")
    public R selectAllStudent() {
        List<DbStudent> studentList = dbStudentService.list(null);
        return R.ok().data("studentList", studentList);
    }

    //2.分页查询所有的学生
    @GetMapping("selectStudentPage/{limit}/{current}")
    @ApiOperation(value = "分页查询所有的学生")
    public R selectStudentPage(@ApiParam(name = "limit", value = "每页显示数") @PathVariable long limit, @ApiParam(name = "current", value = "当前页") @PathVariable long current) {
        Page<DbStudent> dbStudentPage = new Page<>(current, limit);
        dbStudentService.page(dbStudentPage, null);
        return R.ok().data("studentMessage", dbStudentPage.getRecords());
    }

    //3.根据Id删除学生
    @DeleteMapping("deleteStudentByNumber/{Id}")
    @ApiOperation(value = "根据学号删除学生接口测试")
    public R deleteStudentByNumber(@ApiParam(name = "Id", value = "学生Id") @PathVariable String Id) {
        dbStudentService.removeById(Id);
        return R.ok();
    }

    //5.添加学生信息
    @PostMapping("addStudentMessage")
    @ApiOperation(value = "添加学生接口测试")
    public R addStudentMessage(@RequestBody(required = false) DbStudent dbStudent) {
        dbStudentService.save(dbStudent);
        return R.ok();
    }

    //6.修改学生信息
    @PostMapping("updateStudentMessage")
    @ApiOperation(value = "根据ID修改学生信息接口测试")
    public R updateStudentMessage(@RequestBody(required = false) DbStudent dbStudent) {
        dbStudentService.updateById(dbStudent);
        return R.ok();
    }

    //7.带条件的分页查询
    @PostMapping("getStudentMessage/{current}/{limit}")
    @ApiOperation(value = "带条件的分页查询学生信息")
    public R getStudentMessage(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current, @ApiParam(name = "limit", value = "每页显示数", required = true) @PathVariable long limit,
                               @RequestBody(required = false) StudentMessage studentMessage) {
        QueryWrapper<DbStudent> dbStudentQueryWrapper = new QueryWrapper<>();
        //查询的条件
        String number = studentMessage.getStNumber();
        String name = studentMessage.getStName();
        String academy = studentMessage.getStAcademy();
        if (!StringUtils.isEmpty(number)) {
            dbStudentQueryWrapper.eq("St_number", number);
        }
        if (!StringUtils.isEmpty(name)) {
            dbStudentQueryWrapper.like("St_name", name);
        }
        if (!StringUtils.isEmpty(academy)) {
            dbStudentQueryWrapper.eq("St_academy", academy);
        }
        //条件分页查询
        Page<DbStudent> dbStudentPage = new Page<>(current, limit);
        dbStudentService.page(dbStudentPage, dbStudentQueryWrapper);
        List<DbStudent> studentPageRecords = dbStudentPage.getRecords();
        long total = dbStudentPage.getTotal();
        return R.ok().data("Total", total).data("Records", studentPageRecords);
    }
}

